#!/bin/sh

SEATA_DIR=/usr/local/seata/bin

while true; do
  # 检查 seata 目录是否存在
  if [ -d "${SEATA_DIR}" ]; then
    echo "Seata directory found at ${SEATA_DIR}"

    # 查找包含 'seata' 关键字的进程，并获取进程号
    PIDS=$(ps -ef | grep 'seata' | awk '{print $2}')

    # 检查是否有匹配的进程
    if [ -z "$PIDS" ]; then
      echo "No matching processes found"
      cd ${SEATA_DIR} & sh start.sh
    else
      # 循环遍历进程号并 kill -9 进程
      for pid in $PIDS; do
        echo "Killing process $pid"
        kill -9 "$pid"
      done
    fi

  else
    echo "Seata directory not found at ${SEATA_DIR}"
    cd ${SEATA_DIR} & sh start.sh
  fi

  sleep 1
done